package com.nstar.countdown.view.modules.impl
{
	import com.adobe.serialization.json.JSON;
	import com.jwopitz.containers.TileCanvas;
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.ObjectUtils;
	import com.nstar.countdown.ApplicationFacade;
	import com.nstar.countdown.controller.CountDownManager;
	import com.nstar.countdown.view.components.CountDownPanel;
	import com.nstar.countdown.view.itemrenderer.CountDownIcon;
	
	import mx.collections.ArrayCollection;
	import mx.events.ChildExistenceChangedEvent;
	import mx.events.FlexEvent;
	import mx.modules.Module;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class IndexClass extends Module
	{
		public var countDownTop5:RemoteObject;
		public var countDownHot:RemoteObject;
		public var countDownManager:CountDownManager = new CountDownManager("Index");
		
		public var cdpHot:CountDownPanel;
		public var tcHot:TileCanvas;
		public var tcNewList:TileCanvas;
		
		private var logger:Logger = Logger.getInstance(this);
		
		public function IndexClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			var cdma:RemoteObject = countDownManager.findCountDownByHolidayTypeId("A");
			var cdmb:RemoteObject = countDownManager.findCountDownByHolidayTypeId("B");
			
			cdma.addEventListener(ResultEvent.RESULT,resultTop5Handler);
			cdma.addEventListener(FaultEvent.FAULT,faultHandler);
			
			cdmb.addEventListener(ResultEvent.RESULT,resultHotHandler);
			cdmb.addEventListener(FaultEvent.FAULT,faultHandler);	
			
			tcHot.addEventListener(CountDownIcon.SHOW_COUNTDOWN,showCountDown);	
				
		}
		
		private function loadSamples():void{
			var ary:ArrayCollection = ApplicationFacade.getInitProxy().getValue("result");
			tcHot.dataProvider = ary;
			
			cdpHot.dataProvider = ary[0];
			cdpHot.start();
			
			tcNewList.dataProvider = ApplicationFacade.getInitProxy().getValue("result");
		}
		
		//Top5数据
		private function resultTop5Handler(event:ResultEvent):void{
			var result:Array = JSON.decode(event.result as String);
			var ary:ArrayCollection = ObjectUtils.deserializeArray(result,
				ObjectUtils.packageHandlerCallback,"com.nstar.countdown.model.vo");
			tcHot.dataProvider = ary;
			//设置默认的倒计时并运行
			cdpHot.dataProvider = ary[0];
			cdpHot.start();
		}
		
		//热点倒计时数据
		private function resultHotHandler(event:ResultEvent):void{
			var result:Array = JSON.decode(event.result as String);
			var ary:ArrayCollection = ObjectUtils.deserializeArray(result,
				ObjectUtils.packageHandlerCallback,"com.nstar.countdown.model.vo");
			
			tcNewList.dataProvider = ary;
		}		
		
		private function faultHandler(event:FaultEvent):void{
			logger.debugMore("findCountDown() fault",event.fault);
		}
		
		
		private function showCountDown(event:AdvanceEvent):void{
			cdpHot.dataProvider = event.request;
			cdpHot.start();
		}
		
	}
}